package com.vehicle.service.imp;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.vehicle.bean.DataGridView;
import com.vehicle.mapper.AccidentRecordMapper;
import com.vehicle.pojo.AccidentRecord;
import com.vehicle.service.AccidentRecordService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class AccidentRecordServiceImpl implements AccidentRecordService {
    private Logger logger = Logger.getLogger(this.getClass());
    @Autowired
    private AccidentRecordMapper accidentRecordMapper;
    @Override
    public DataGridView findAccidentRecordPage(AccidentRecord accidentRecord) {
        DataGridView dataGridView = new DataGridView();
        Page page= PageHelper.startPage(accidentRecord.getPage(),accidentRecord.getLimit());
        dataGridView.setData(accidentRecordMapper.selectAccidentRecord(accidentRecord));
        dataGridView.setCount(page.getTotal());
        return dataGridView;
    }

    @Override
    public void deleteAccident(int accidentid) {
        accidentRecordMapper.updateStatusByAccidentId(accidentid);
    }

    @Override
    public void deleteBatchAccidentRecord(Integer[] accidentIds) {
        for(Integer accidentId:accidentIds){
            accidentRecordMapper.updateStatusByAccidentId(accidentId);
        }
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void addAccidentRecord(AccidentRecord accidentRecord) {
        accidentRecordMapper.addAccidentRecord(accidentRecord);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateAccidentRecord(AccidentRecord accidentRecord) {
        accidentRecordMapper.updateAccidentRecord(accidentRecord);
    }
}
